//
//  PhoneValidViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/23.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "PhoneValidViewController.h"
#import "PhoneChangeViewController.h"

@interface PhoneValidViewController ()

@property (strong, nonatomic) UIView *phoneView;
@property (strong, nonatomic) UILabel *phoneL;
@property (strong, nonatomic) UILabel *phone;
@property (strong, nonatomic) UIView *line1;


@property (strong, nonatomic) UIView *codeView;
@property (strong, nonatomic) UILabel *codeL;
@property (strong, nonatomic) UITextField *codeTextField;
@property (strong, nonatomic) UIButton *getCodeBtn;
@property (strong, nonatomic) UIView *line2;


@property (strong, nonatomic) UIImageView *nextIV;
@property (strong, nonatomic) UILabel *nextL;


@end

@implementation PhoneValidViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WhiteColor;
    self.title = @"修改手机号码";
 
    [self createUI];
}

#pragma mark -下一步
- (void)nextClick:(UIButton *)btn {
    
    NSLog(@"下一步");
    
    
    if (self.codeTextField.text.length <= 0) {
        
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入验证码"];
        return ;
    
    }
    
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"user_mobile\" : \"%@\", \"code\" : \"%@\"}", [RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey], [RSA encryptString:[CommonUserInfo sharedInstance].phone publicKey:PublicKey], [RSA encryptString:self.codeTextField.text publicKey:PublicKey]];
    
    [PPNetworkHelper POST:@"user/User/Updateusermobile_ver" parameters:@{@"json" : par} success:^(id responseObject) {
        
        NSLog(@"旧手机号验证：%@", responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            PhoneChangeViewController *vc = [[PhoneChangeViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
    }];
    
   
}

- (void)createUI {
    
    [self.view addSubview:self.phoneView];
    [self.phoneView addSubview:self.phoneL];
    [self.phoneView addSubview:self.phone];
    [self.phoneView addSubview:self.line1];
    
    [self.view addSubview:self.codeView];
    [self.codeView addSubview:self.codeL];
    [self.codeView addSubview:self.codeTextField];
    [self.codeView addSubview:self.getCodeBtn];
    [self.codeView addSubview:self.line2];
    
    [self.view addSubview:self.nextIV];
    [self.view addSubview:self.nextL];
    
    __weak typeof(self) weakSelf = self;
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.height.mas_equalTo(jisuan(92));
    }];
    [self.phoneL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.phoneView.mas_left).with.offset(jisuan(40));
        make.centerY.mas_equalTo(weakSelf.phoneView.mas_centerY);
        make.width.mas_equalTo(jisuan(76));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.phone mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.phoneL.mas_right).with.offset(jisuan(40));
        make.centerY.mas_equalTo(weakSelf.phoneL.mas_centerY);
    }];
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.phoneView.mas_left).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.phoneView.mas_right).with.offset(-jisuan(40));
        make.bottom.mas_equalTo(weakSelf.phoneView.mas_bottom);
        make.height.mas_equalTo(jisuan(2));
    }];
    
    
    [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.phoneView.mas_bottom);
        make.height.mas_equalTo(jisuan(92));
    }];
    [self.codeL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.codeView.mas_left).with.offset(jisuan(40));
        make.centerY.mas_equalTo(weakSelf.codeView.mas_centerY);
        make.width.mas_equalTo(jisuan(76));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.codeView.mas_right).with.offset(-jisuan(40));
        make.centerY.mas_equalTo(weakSelf.codeL.mas_centerY);
        make.width.mas_equalTo(jisuan(130));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.getCodeBtn.mas_left).with.offset(-jisuan(40));
        make.centerY.mas_equalTo(weakSelf.codeView.mas_centerY);
        make.left.mas_equalTo(weakSelf.codeL.mas_right).with.offset(jisuan(40));
        make.height.mas_equalTo(jisuan(34));
    }];
    
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.codeView.mas_left).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.codeView.mas_right).with.offset(-jisuan(40));
        make.bottom.mas_equalTo(weakSelf.codeView.mas_bottom);
        make.height.mas_equalTo(jisuan(2));
    }];
    
    [self.nextIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.top.mas_equalTo(weakSelf.codeView.mas_bottom).with.offset(jisuan(98));
        make.width.mas_equalTo(jisuan(550));
        make.height.mas_equalTo(jisuan(106));
    }];
    [self.nextL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.nextIV.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.nextIV.mas_centerY).with.offset(-jisuan(4));
    }];
    
}

#pragma mark -获取验证码
- (void)getCodeBtnClick:(UIButton *)btn {
    
    NSLog(@"获取验证码");
    
    [[CommonAction sharedInstance] getCodeWithPhone:self.phone.text type:@"updatephone" andSender:btn phoneCodeBlock:^(NSString *codeStr) {
        
        NSLog(@"修改手机号旧手机号验证码获取：%@", codeStr);
        
    }];
}

#pragma mark -lazy
- (UIView *)phoneView {
    
    if (!_phoneView) {
        
        _phoneView = [[UIView alloc] init];
        _phoneView.backgroundColor = WhiteColor;
    }
    
    return _phoneView;
}
- (UILabel *)phoneL {
    
    if (!_phoneL) {
        
        _phoneL = [[UILabel alloc] init];
        _phoneL.text = @"手机号";
        _phoneL.textColor = Color333333;
        _phoneL.font = Font(24);
        _phoneL.textAlignment = NSTextAlignmentLeft;
        
    }
    return _phoneL;
}
- (UILabel *)phone {
    
    if (!_phone) {
        
        _phone = [[UILabel alloc] init];
        _phone.textColor = Color333333;
        _phone.font = Font(24);
        _phone.textAlignment = NSTextAlignmentLeft;
//        _phone.text = @"150***2333";
        _phone.text = [CommonUserInfo sharedInstance].phone;
        
    }
    return _phone;
}
- (UIView *)line1 {
    
    if (!_line1) {
        
        _line1 = [[UIView alloc] init];
        _line1.backgroundColor = Colordddddd;
    }
    return _line1;
}


- (UIView *)codeView {
    
    if (!_codeView) {
        
        _codeView = [[UIView alloc] init];
        _codeView.backgroundColor = WhiteColor;
    
    }
    
    return _codeView;
}
- (UILabel *)codeL {
    
    if (!_codeL) {
        
        _codeL = [[UILabel alloc] init];
        _codeL.text = @"验证码";
        _codeL.textColor = Color333333;
        _codeL.font = Font(24);
        _codeL.textAlignment = NSTextAlignmentLeft;
        
    }
    return _codeL;
}
- (UITextField *)codeTextField {
    
    if (!_codeTextField) {
        
        _codeTextField = [[UITextField alloc] init];
        _codeTextField.textColor = Color333333;
        _codeTextField.font = Font(24);
        _codeTextField.textAlignment = NSTextAlignmentLeft;
        _codeTextField.placeholder = @"请输入验证码";
        
        _codeTextField.keyboardType = UIKeyboardTypeDefault;
        
    }
    return _codeTextField;
}
- (UIButton *)getCodeBtn {
    
    if (!_getCodeBtn) {
        
        _getCodeBtn = [[UIButton alloc] init];
        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateSelected];
        
        [_getCodeBtn setTitleColor:Colorff4200 forState:UIControlStateSelected];
        [_getCodeBtn setTitleColor:Colorff4200 forState:UIControlStateNormal];
        
        _getCodeBtn.titleLabel.font = Font(24);
        
        [_getCodeBtn addTarget:self action:@selector(getCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _getCodeBtn;
}


- (UIView *)line2 {
    
    if (!_line2) {
        
        _line2 = [[UIView alloc] init];
        _line2.backgroundColor = Colordddddd;
    }
    return _line2;
}

- (UIImageView *)nextIV {
    
    if (!_nextIV) {
        
        _nextIV = [[UIImageView alloc] init];
        _nextIV.image = [UIImage imageNamed:@"redBtnBG"];
        _nextIV.contentMode = UIViewContentModeScaleAspectFit;
        
        _nextIV.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextClick:)];
        [_nextIV addGestureRecognizer:recognizer];
        
    }
    
    return _nextIV;
}

- (UILabel *)nextL {
    
    if (!_nextL) {
        
        _nextL = [[UILabel alloc] init];
        _nextL.text = @"下一步";
        _nextL.textColor = WhiteColor;
        _nextL.font = Font(32);
        _nextL.textAlignment = NSTextAlignmentCenter;
    }
    return _nextL;
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
//    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
